import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入路由
import Longin from '@/views/login/Login.vue'
import Home from '@/views/login/Home.vue'
import Index from '@/views/index/Index.vue'
import prdt from '@/views/index/prdt.vue'
import fixedAssets from '@/views/index/fixedAssets.vue'
import cartCheck from '@/views/index/cartCheck.vue'
import checkDetail from '@/views/index/checkDetail.vue'
import printDevice from '@/views/index/printDevice.vue'
import text from '@/views/text.vue'

Vue.use(VueRouter)

const routes = [
    { path: '/', redirect: '/login' },
    { path: '/login', component: Longin },
    { path: '/text', component: text },
    {
        path: '/Home',
        component: Home,
        redirect: '/index',
        children: [
            {
                path: '/index', component: Index, meta: {
                    keepAlive: true, // 是否缓存   
                    scollTopPosition: 0 // 滚动条位置
                }
            }, {
                path: '/prdt', component: prdt, meta: {
                    keepAlive: true, // 是否缓存   
                    scollTopPosition: 0 // 滚动条位置
                }
            }, {
                path: '/fixedassets', component: fixedAssets, meta: {
                    keepAlive: true, // 是否缓存   
                    scollTopPosition: 0 // 滚动条位置
                }
            },
            {
                path: '/cartcheck', component: cartCheck, meta: {
                    keepAlive: true, // 是否缓存   
                    scollTopPosition: 0 // 滚动条位置
                }
            } , {
                path: '/checkdetail', component: checkDetail, meta: {
                    keepAlive: true, // 是否缓存   
                    scollTopPosition: 0 // 滚动条位置
                }
            }
            ,{
                path: '/printdevice', component: printDevice, meta: {
                    keepAlive: true, // 是否缓存   
                    scollTopPosition: 0 // 滚动条位置
                }
            },
        ]
    }
]

const router = new VueRouter({
    routes
})

// 挂载路由守卫
router.beforeEach((to, from, next) => {
    // to将要访问的路径
    // from从哪个路径跳转而来
    // next是一个函数表示放行，调用方式（1）next()放行，（2）next('/login') 不放行强行跳转页面
    // 如果访问的是login页面则可以直接放行
    if (to.path === '/text') return next()
    if (to.path === '/login') return next()
    // 如果不是login页面则需要进行判断看有没有tokon,token是登录成功够保存到sessionSlotr里面的值
    // 获取token的值
    const tokenStr = window.sessionStorage.getItem('token') // eslint-disable-line no-unused-vars
    // 判断token值是否存在,不存在直接跳转到登录页面
    if (!tokenStr) return next('/login')
    // 存在世界放行
    next()
})

export default router